package com.mms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.mms.entity.Customer;
import com.mms.entity.CustomerState;
import com.mms.entity.Gender;
import com.mms.entity.InfoSource;
import com.mms.util.JdbcUtils;

public class CustomerMapperImpl implements CustomerMapper {

	@Override
	public void updateByPrimaryKey(Customer customer, Connection conn) {
		String sql = "UPDATE `customer` \r\n" + 
				"SET `owner_username` = ?,\r\n" + 
				"`name` = ?,\r\n" + 
				"`gender` = ?,\r\n" + 
				"`address` = ?,\r\n" + 
				"`phone` = ?,\r\n" + 
				"`source` = ?,\r\n" + 
				"`state` = ?,\r\n" + 
				"`deal_date` = ?,\r\n" + 
				"`last_follow_up_date` = ?,\r\n" + 
				"`last_follow_up_note` = ? \r\n" + 
				"WHERE\r\n" + 
				"	`cust_id` = ?";
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, customer.getOwnerUsername());
			stmt.setString(2, customer.getName());
			stmt.setString(3, customer.getGender().name());
			stmt.setString(4, customer.getAddress());
			stmt.setString(5, customer.getPhone());
			stmt.setString(6, customer.getSource().name());
			stmt.setString(7, customer.getState().name());
			stmt.setDate(8, new java.sql.Date(customer.getDealDate().getTime()));
			stmt.setDate(9, new java.sql.Date(customer.getLastFollowUpDate().getTime()));
			stmt.setString(10, customer.getLastFollowUpNote());
			stmt.setInt(11, customer.getCustId());
			stmt.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtils.close(stmt);
		}
	}

	@Override
	public Customer selectByPrimaryKey(int custId) {
		String sql = "SELECT * FROM customer WHERE cust_id = ?";
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Customer c = null;
		try {
			conn = JdbcUtils.getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, custId);
			rs = stmt.executeQuery();
			if (rs.next()) {
				c = new Customer();
				String ownerUsername = rs.getString("owner_username");
				String name = rs.getString("name");
				Gender gender = Enum.valueOf(Gender.class, rs.getString("gender"));
				String address = rs.getString("address");
				String phone = rs.getString("phone");
				InfoSource source = Enum.valueOf(InfoSource.class, rs.getString("source"));
				CustomerState state = Enum.valueOf(CustomerState.class, rs.getString("state"));
				Date dealDate = rs.getDate("deal_date");
				Date lastFollowUpDate = rs.getDate("last_follow_up_date");
				String lastFollowUpNote = rs.getString("last_follow_up_note");
				c.setCustId(custId);
				c.setOwnerUsername(ownerUsername);
				c.setName(name);
				c.setGender(gender);
				c.setAddress(address);
				c.setPhone(phone);
				c.setSource(source);
				c.setState(state);
				c.setDealDate(dealDate);
				c.setLastFollowUpDate(lastFollowUpDate);
				c.setLastFollowUpNote(lastFollowUpNote);
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtils.close(stmt, conn);
		}
		return c;
	}

}
